/**
@Description  请填写文件描述（需要改）
@author: freedom
@date: 2021/5/25
@note:
**/
package v1

import (
	"encoding/json"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strings"
	"time"
)

// 创建文档
func CreateKnowledge(c *gin.Context) {
	// form映射
	var sysKnowledge model.SysKnowledge
	err := c.ShouldBindJSON(&sysKnowledge)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	// form数据验证
	if err := utils.Verify(sysKnowledge, utils.SysKnowledge); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	now := time.Now().UnixNano() / 1e6
	if sysKnowledge.Id != "" {
		sysKnowledge.UpdateTime = now
		// 赋值操作用户
		_, user := service.FindUserById(utils.GetUserIdByRequest(*c))
		if !strings.Contains(sysKnowledge.Author, user.NickName) {
			sysKnowledge.Author = sysKnowledge.Author + " " + user.NickName
		}
		b, _ := json.Marshal(&sysKnowledge)
		var m map[string]interface{}
		json.Unmarshal(b, &m)
		// 更新文档
		err = service.UpdateDocument(sysKnowledge.Id, m)
		if err != nil {
			global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
			response.FailWithMessage("更新失败", c)
		} else {
			response.OkWithMessage("更新成功", c)
		}
	} else {
		sysKnowledge.CreateTime = now
		sysKnowledge.UpdateTime = now
		// 赋值操作用户
		_, user := service.FindUserById(utils.GetUserIdByRequest(*c))
		sysKnowledge.Author = user.NickName
		// 唯一性校验
		id := service.QueryKnowledge(sysKnowledge.Title)
		if id != "" {
			response.FailWithMessage("标题已存在，请勿重复", c)
			return
		}

		// 创建文档
		err = service.CreateKnowledge(sysKnowledge)
		if err != nil {
			global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
			response.FailWithMessage("创建失败", c)
		} else {
			response.OkWithMessage("创建成功", c)
		}
	}

}

// 根据标题查询
func QueryKnowledgeByTitle(c *gin.Context) {
	// json form 映射
	var sysKnowledge model.SysKnowledge
	err := c.ShouldBindJSON(&sysKnowledge)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	// 唯一性校验
	id := service.QueryKnowledge(sysKnowledge.Title)
	if id != "" {
		response.OkWithData("标题已存在，请勿重复", c)
	} else {
		response.OkWithData("记录不存在", c)
	}

}

// 根据条件查询
func QueryKnowledgeBySearch(c *gin.Context) {
	var pageInfo request.SysKnowledgeSearch
	// 获取get query 参数 问题：sysknowledges 绑定失败
	title := c.DefaultQuery("title", "")
	var tags []string
	strs := c.DefaultQuery("tags", "")
	if strs != "" {
		if strings.Contains(strs, ",") {
			for _, value := range strings.Split(strs, ",") {
				tags = append(tags, value)
			}
		} else {
			tags = append(tags, strs)
		}
	}
	// 绑定query 参数
	_ = c.ShouldBindQuery(&pageInfo)
	pageInfo.Title = title
	pageInfo.Tags = tags
	if list, total, err := service.QueryKnowledgesBySearch(pageInfo); err != nil {
		global.GVA_LOG.Error("查询失败", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// 根据条件回显提示

func QuerySuggestByTitle(c *gin.Context) {
	var sysKnowledge model.SysKnowledge
	err := c.ShouldBindJSON(&sysKnowledge)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, err := service.QuerySuggestByTitle(sysKnowledge); err != nil {
		global.GVA_LOG.Error("查询失败", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List: list,
		}, "获取成功", c)
	}
}
